let app = getApp();
Page({
    data: {
        loginImg: "/public/icon/luockoo_logo.png",
        checkIcon: "/public/icon/select_gray.png",
        checkedIcon: "/public/icon/check_on.png",
        selectFlag: false,
        count: 60,
        userNumber: "", // 手机号
        code: "", 
        password: "",
        codeFlag: true
    },
    selectFn: function () {
        let selectFlag = !this.data.selectFlag;
        this.setData({
            selectFlag: selectFlag
        })
    },
    numberFn: function (e) {
          let userNumber = e.detail.value;
          this.setData({
              userNumber: userNumber
          })
    
    },
    codeFn: function (e) {
          let code = e.detail.value;
          this.setData({
              code: code
          
          })
    },
    passwordFn: function (e) {
          let password = e.detail.value;
          this.setData({        
              password: password


          })
    },
    getCodeFn: function () {
        let _this = this;

        let userNumber = this.data.userNumber;
        let storeId =  1;
        let storeType = 6;
        let reg = /^[1][3456789][0-9]{9}$/;
        if (!reg.test(userNumber)) {
            _this.showErrorFn("请输入正确手机号", "none")
            return ;
        
        }
        this.setData({
            codeFlag: false
        })
        let set = setInterval(()=>{
            let count = _this.data.count;
            if (count >= 1) {
               count -= 1;
               _this.setData({
                    count: count
                })      
            } else {
                clearInterval(set)
                _this.setData({
                    count: 60,  
                    codeFlag: true
                })
            }
        }, 1000)
        wx.request({
          url: app.globalData.url, 
          method: "get",
          data: {
              api: "app.user.sendSms",
              storeId: 1,
              storeType: 6,
              phone:  userNumber,
              smsType: 2
          },
          header: {
            'content-type': 'application/json' 
          },
          success (res) {
            if (res.data && res.data.code == 200) {
                  let resData = res.data.data;
              } else {
                  _this.showErrorFn(res.data.message, "none")
              }
          }
        })
    },
    showErrorFn: function (errorMsg, errorStatus) {
        wx.showToast({
            title: errorMsg,
            icon: errorStatus || "error",
            mask: true
        })
    },
    registerFn: function () {
      let _this = this;
      let userNumber = this.data.userNumber;
      let code = this.data.code;
      let password = this.data.password; 
      let selectFlag = this.data.selectFlag;
      let storeId =  1;
      let storeType = 6;
      let reg = /^[1][3456789][0-9]{9}$/;
      if (!reg.test(userNumber)) {
          _this.showErrorFn("请输入正确手机号", "none")
          return ;
      
      }
    
      if (!code) {
        _this.showErrorFn("请输入验证码")
        return ;
      }

      if (!password) {
          _this.showErrorFn("请输入密码")
          return ;
      }
      if (!selectFlag) {
           wx.showModal({   
                content: '我已阅读并同意《珞珂家居平台商户入驻协议》《信息数据收集协议》',
                cancelText: "不同意",


                confirmText: "同意",
                success (res) {
                  if (res.confirm) {        
                      _this.setData({
                          selectFlag: true
                      }, function () {
                          _this.registerRequestFn(userNumber, code, password)
                      })
                  } else if (res.cancel) {
                      console.log('用户点击取消')
                  }
                }
            })
        
        } else {
          _this.registerRequestFn(userNumber, code, password)
        }
    },
    registerRequestFn: function (userNumber, code, password) {
      wx.request({
        url: app.globalData.url, 
        method: "get",
        data: {
            api:'app.login.register',
            storeId: 1,
            storeType: 6,
            phone: userNumber,
            keyCode: code,
            password: password,
            userType:  2
        },
        header: {
          'content-type': 'application/json' 
        },
        success (res) {
             console.log(res.data)
            if (res.data && res.data.code == 200) {
                let resData = res.data.data;
                wx.showToast({
                  title: "注册成功",
                  icon: "success",
                  mask: true
                })
                setTimeout(()=> {
                    wx.navigateTo({
                      url: '/pages/numberlogin/numberlogin',
                    })
                }, 2000)
            } else if (res.data && res.data.code == 50894) {
                wx.showToast({
                    title: res.data.message,
                    icon: "error"
                })
                setTimeout(()=> {
                  wx.navigateTo({
                    url: '/pages/numberlogin/numberlogin',
                  })
              }, 2000)
            } else {
              wx.showToast({
                title: res.data.message,
                icon: "error"
              })
            }
        }
      })
    }
})